﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using TileEngine;

namespace CarlosMisfortune.model
{
    public static class Collision
    {
        public static Vector2 HorizontalCollisionTest(Vector2 a_moveAmount, GameObject obj)
        {
            if (a_moveAmount.X == 0)
            {
                return a_moveAmount;
            }

            Rectangle afterMoveRect = obj.CollisionRectangle;
            afterMoveRect.Offset((int)a_moveAmount.X, 0);

            Vector2 corner1, corner2;

            if (a_moveAmount.X < 0)
            {
                corner1 = new Vector2(afterMoveRect.Left, afterMoveRect.Top + 1);
                corner2 = new Vector2(afterMoveRect.Left, afterMoveRect.Bottom - 1);                                    
            }
            else
            {
                corner1 = new Vector2(afterMoveRect.Right, afterMoveRect.Top + 1);
                corner2 = new Vector2(afterMoveRect.Right, afterMoveRect.Bottom - 1);                                    
            }
            
            Vector2 mapCell1 = TileMap.GetCellByPixel(corner1);
            Vector2 mapCell2 = TileMap.GetCellByPixel(corner2);

            if (TileMap.CellIsBlocked(mapCell1) ||
                TileMap.CellIsBlocked(mapCell2))
            {
                a_moveAmount.X = 0;
                obj.VelocityX = 0;
            }            

            if (!obj.IsPlayer)
            {
                if (TileMap.CellTypeValue(mapCell1) == "BLOCK" ||
                    TileMap.CellTypeValue(mapCell2) == "BLOCK")
                {
                    a_moveAmount.X = 0;
                    obj.VelocityX = 0;
                }
            }

            return a_moveAmount;
        }

        public static Vector2 VerticalCollisionTest(Vector2 a_moveAmount, GameObject obj)
        {
            if (a_moveAmount.Y == 0)
            {
                return a_moveAmount;
            }

            Rectangle afterMoveRect = obj.CollisionRectangle;
            afterMoveRect.Offset((int)a_moveAmount.X, (int)a_moveAmount.Y);

            Vector2 corner1, corner2;

            if (a_moveAmount.Y < 0)
            {
                corner1 = new Vector2(afterMoveRect.Left + 1, afterMoveRect.Top);
                corner2 = new Vector2(afterMoveRect.Right -1, afterMoveRect.Top);
            }
            else
            {
                corner1 = new Vector2(afterMoveRect.Left + 1, afterMoveRect.Bottom);
                corner2 = new Vector2(afterMoveRect.Right - 1, afterMoveRect.Bottom);
            }

            Vector2 mapCell1 = TileMap.GetCellByPixel(corner1);
            Vector2 mapCell2 = TileMap.GetCellByPixel(corner2);

            if (TileMap.CellIsBlocked(mapCell1) ||
                TileMap.CellIsBlocked(mapCell2))
            {
                if (a_moveAmount.Y > 0)
                {
                    obj.OnGround = true;
                }
                a_moveAmount.Y = 0;
                obj.VelocityY = 0;
            }

            if (!obj.IsPlayer)
            {
                if (TileMap.CellTypeValue(mapCell1) == "BLOCK" ||
                    TileMap.CellTypeValue(mapCell2) == "BLOCK")
                {
                    if (a_moveAmount.Y > 0)
                    {
                        obj.OnGround = true;
                    }

                    a_moveAmount.Y = 0;
                    obj.VelocityY = 0;
                }
            }

            return a_moveAmount;
        }
    }
}
